require 'net/https' 
require 'iconv'
def login(uname,pass,typename,domainid)
  begin
    logout(uname,pass,domainid)
    kickall(uname,pass,domainid)
    url = URI.parse('https://auth.gucas.ac.cn/php/user_login.php') 
    http = Net::HTTP.new(url.host, url.port)  
    http.use_ssl = true 
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    postdata="logintype=#{typename}&loginuser=#{uname}&domainid=#{domainid}&password=#{pass}"  #logininner
    res,resdata= http.post(url.path,postdata)
    cookie = res.response['set-cookie']
    headers = {'Cookie' => cookie,'Refer'=>"https://auth.gucas.ac.cn/php/user_login.php",'Content-Type' => 'application/x-www-form-urlencoded'} 
    url=URI.parse("https://auth.gucas.ac.cn"+res.body.match(/href.+\'(.+)\'\;/)[1])
    res,resdata=http.get(url.path+"?"+url.query,headers)
    if resdata.match(/\xC1\xAC\xCF\xDF\xB3\xC9\xB9\xA6/)!=nil
      return true
    end
  rescue 
    return false
  end
end

def translate(str)
    url = URI.parse('https://ajax.googleapis.com/ajax/services/language/translate') 
    para="?v=1.0&q="
    str.split("\n").each do |value|
      begin
        para=para+URI.encode(Iconv.iconv("UTF-8","GBK",value+"___")[0])
      rescue
        para=para+URI.encode(Iconv.iconv("UTF-8","BIG5",value+"___")[0])
      end
    end
    para=para+"&langpair=zh-CN%7Cen"    
    http = Net::HTTP.new(url.host, url.port)  
    http.use_ssl = true 
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    res,resdata= http.get(url.path+para)
    if resdata.match(/translatedText\":\"(.+)\"\}\,/)
      return resdata.match(/translatedText\":\"(.+)\"\}\,/)[1]
    end
    return ""
end


def logout(uname,pass,domainid)
  begin
    url = URI.parse('https://auth.gucas.ac.cn/php/user_login.php') 
    http = Net::HTTP.new(url.host, url.port)  
    http.use_ssl = true 
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    postdata="logintype=\xC0\xEB\xCF\xDF&loginuser=#{uname}&domainid=#{domainid}&password=#{pass}"  
    res,resdata= http.post(url.path,postdata)
    cookie = res.response['set-cookie']
    headers = {'Cookie' => cookie,'Refer'=>"https://auth.gucas.ac.cn/php/user_login.php",'Content-Type' => 'application/x-www-form-urlencoded'} 
    url=URI.parse("https://auth.gucas.ac.cn"+res.body.match(/href.+\'(.+)\'\;/)[1])
    res,resdata=http.get(url.path+"?"+url.query,headers)
    if resdata.match(/\xC0\xEB\xCF\xDF\xB3\xC9\xB9\xA6/)!=nil
      return true
    end
  rescue 
    return false
  end
end

def selfservice(uname,pass,domainid)
  begin
    url = URI.parse('https://auth.gucas.ac.cn/php/user_login.php') 
    http = Net::HTTP.new(url.host, url.port)  
    http.use_ssl = true 
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    postdata="logintype=\327\324\267\376\316\361&loginuser=#{uname}&domainid=#{domainid}&password=#{pass}"  
    res,resdata= http.post(url.path,postdata)  
    cookie = res.response['set-cookie']
    if resdata.match(/\xBB\xB6\xD3\xAD\xC4\xFA\xCA\xB9\xD3\xC3\xBC\xC6\xB7\xD1\xCF\xB5\xCD\xB3/)!=nil
      return cookie
    end
  rescue 
    return false
  end
end

def onlinelist(uname,pass,domainid)
  begin
    url=URI.parse("https://auth.gucas.ac.cn/php/useronlinelist.php")
    http = Net::HTTP.new(url.host, url.port)  
    http.use_ssl = true 
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    cookie = selfservice(uname,pass,domainid)
    if cookie==false
      return false
    end
    headers = {'Cookie' => cookie,'Refer'=>"https://auth.gucas.ac.cn/php/user_login.php",'Content-Type' => 'application/x-www-form-urlencoded'} 
    res,resdata=http.get(url.path,headers)
    if resdata.match(/\xB5\xC7\xC2\xBC\xC1\xD0\xB1\xED/)!=nil
      return resdata
    end
  rescue
    return false
  end
end

def kickall(uname,pass,domainid)
  begin
    url=URI.parse("https://auth.gucas.ac.cn/php/kickself.php")
    http = Net::HTTP.new(url.host, url.port)  
    http.use_ssl = true 
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    if (onlineip=onlinelist(uname,pass,domainid))==false
      return false
    end
    onlineip=onlineip.scan(/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/)
    if onlineip!=nil
      onlineip.each do |eip|
        cookie = selfservice(uname,pass,domainid)
        headers = {'Cookie' => cookie,'Refer'=>"https://auth.gucas.ac.cn/php/user_login.php",'Content-Type' => 'application/x-www-form-urlencoded'} 
        postdata="ip=#{eip}&password=#{pass}"
        res,resdata=http.post(url.path,postdata,headers)
      end
    end
 rescue
    return false
  end
end


def useamount(uname,pass,typename,domainid)
  begin
    if login(uname,pass,typename,domainid)==false
      return false
    end
    url=URI.parse("https://auth.gucas.ac.cn/php/onlinestatus.php")
    http = Net::HTTP.new(url.host, url.port)  
    http.use_ssl = true 
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE	
    cookie = selfservice(uname,pass,domainid)
    if cookie==false
      return false
    end
    headers = {'Cookie' => cookie,'Refer'=>"https://auth.gucas.ac.cn/php/user_login.php",'Content-Type' => 'application/x-www-form-urlencoded'} 
    res,resdata=http.get(url.path,headers)
    returndata=Hash.new
    tempdata=resdata.scan(/\>.*Mb/).to_s.gsub(",","").scan(/[0-9]+\.[0-9]+/)
    returndata["outupload"]=tempdata[4]
    returndata["outdownload"]=tempdata[5]
    returndata["inupload"]=tempdata[2]
    returndata["indownload"]=tempdata[3]
    returndata["gucasupload"]=tempdata[0]
    returndata["gucasdownload"]=tempdata[1]
    returndata["usefee"]=resdata.match(/.+\>(.+)\s\xd4\xaa/)[1].gsub(",","")
    return returndata
  rescue 
    return false
  end
end

def passwdchange(uname,pass,newpass,domainid)
  begin
    url = URI.parse('https://auth.gucas.ac.cn/php/user_chgpass.php') 
    http = Net::HTTP.new(url.host, url.port)  
    http.use_ssl = true 
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    cookie=selfservice(uname,pass,domainid)
    headers = {'Cookie' => cookie,'Refer'=>"https://auth.gucas.ac.cn/php/user_login.php",'Content-Type' => 'application/x-www-form-urlencoded'} 
    postdata="password=#{pass}&new=#{newpass}&confirm=#{newpass}"
    res,resdata=http.post(url.path,postdata,headers)
    if resdata.match(/\xB3\xC9\xB9\xA6/)!=nil
      return true
    end
  rescue 
    return false
  end
end

def remainfee(uname,pass,domainid)
  begin
    url = URI.parse('https://auth.gucas.ac.cn/php/remain_fee.php') 
    http = Net::HTTP.new(url.host, url.port)  
    http.use_ssl = true 
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    cookie=selfservice(uname,pass,domainid)
    if cookie==false
      return false
    end
    headers = {'Cookie' => cookie,'Refer'=>"https://auth.gucas.ac.cn/php/user_login.php",'Content-Type' => 'application/x-www-form-urlencoded'} 
    res,resdata=http.get(url.path,headers)
    return resdata.match(/\<br\>\<b\>(.+)\<\/b/)[1].gsub(",","")
  rescue 
    return false
  end
end


def viewmyself(uname,pass,domainid)
  begin
    url = URI.parse('https://auth.gucas.ac.cn/php/viewmyself.php') 
    http = Net::HTTP.new(url.host, url.port)  
    http.use_ssl = true 
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    cookie=selfservice(uname,pass,domainid)
    if cookie==false
      return false
    end
    headers = {'Cookie' => cookie,'Refer'=>"https://auth.gucas.ac.cn/php/user_login.php",'Content-Type' => 'application/x-www-form-urlencoded'} 
    res,resdata=http.get(url.path,headers)
    dataname=resdata.scan(/align\=\"right\"\s+width\=\"50\%.*\"\>(.+)\</)
    datavalue=resdata.scan(/colspan\=\"2\"\s+width\=\"50\%.*\"\.*>(.+)\</)
    returndata=[]
    0.upto(dataname.length-1) do |i|
      returndata[i]=dataname[i]+datavalue[i]
    end
    returndata=returndata.join('$$').gsub(/\241\241+/,"").split('$$')
    return returndata
  rescue 
    return false
  end
end


def viewmygroup(uname,pass,domainid)
  begin
    url = URI.parse('https://auth.gucas.ac.cn/php/viewmygroup.php') 
    http = Net::HTTP.new(url.host, url.port)  
    http.use_ssl = true 
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    cookie=selfservice(uname,pass,domainid)
    if cookie==false
      return false
    end
    headers = {'Cookie' => cookie,'Refer'=>"https://auth.gucas.ac.cn/php/user_login.php",'Content-Type' => 'application/x-www-form-urlencoded'} 
    res,resdata=http.get(url.path,headers)
    returndata=Hash.new
    tempdata=resdata.scan(/\xCF\xC2\xD0\xD0:(.+)\xd4\xaa/)
    returndata["outfee"]=tempdata[0][0]
    returndata["infee"]=tempdata[1][0]
    tempdata=resdata.scan(/\xCF\xC2\xD0\xD0:(.+)G/)
    returndata["outamount"]=tempdata[0][0]
    returndata["inamount"]=tempdata[1][0]
    return returndata
  rescue 
    return false
  end
end

  

